from django.db import models
from mdeditor.fields import MDTextField
from django.contrib.auth.models import User

from django.contrib.auth.models import AbstractUser


class MyUser(AbstractUser):
    phone_number = models.CharField("电话",max_length=20)
    userinfo = models.TextField('简介', max_length=200, blank=True)

    img = models.ImageField(upload_to="user_logo/%Y/%m/%d/",
                            verbose_name='上传头像',
                            blank=True, null=True)

class ArticleClass(models.Model):
    name = models.CharField("栏目名称", max_length=100)
    index = models.IntegerField(default=999, verbose_name='栏目排序')


    class Meta:
        verbose_name = '栏目分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
# 表名：Tui、推荐位名name。
class Tui(models.Model):
    name = models.CharField('推荐位', max_length=100)
    id = models.AutoField(primary_key=True)

    class Meta:
        verbose_name = "推荐位"
        verbose_name_plural = "推荐位"

    def __str__(self):
        return self.name
# 表名：Article、标题：title、摘要：excerpt、分类：category、标签：tags、
# 推荐位、内容：body、创建时间：created_time、作者：user、文章封面图片img
class Article(models.Model):
    STATUS_CHOICES = (
        ('d', '草稿'),
        ('p', '发表'),
    )
    
    title = models.CharField('标题', max_length=50)
    excerpt = models.TextField('摘要', max_length=200, blank=True)

    # 使用外键关联标签表与标签是多对多关系

    # content = models.TextField('内容', default="")
    content = MDTextField()  # markdown编辑器

    # 文章作者，这里User是从django.contrib.auth.models导入的。
    # 这里我们通过 ForeignKey 把文章和 User 关联了起来。
    # user = models.ForeignKey(User,
    #                          on_delete=models.CASCADE,  # 删除关联数据，与之关联也删除
    #                          verbose_name='作者')
# 
    # user = models.CharField('作者', max_length=50,default="admin")
    user = models.ForeignKey(MyUser, on_delete=models.DO_NOTHING, 
                            verbose_name='用户',
                            blank=True, null=True,related_name="user")


    views = models.PositiveIntegerField('阅读量', default=0)
    tui = models.ForeignKey(Tui, on_delete=models.DO_NOTHING,  # 删除关联数据,什么也不做
                            verbose_name='推荐位',
                            blank=True, null=True,related_name="tuiKey")

    Class = models.ForeignKey(ArticleClass, on_delete=models.DO_NOTHING,  # 删除关联数据,什么也不做
                            verbose_name='栏目',
                            blank=True, null=True,related_name="classKey")


    status = models.CharField(verbose_name='状态', max_length=1, choices=STATUS_CHOICES, default='p')

    # auto_now_add
    # 会在model对象第一次被创建时，将字段的值设置为创建时的时间，以后修改对象时，字段的值不会再更新。
    created_time = models.DateTimeField('发布时间', auto_now_add=True)
    # auto_now
    # 将其值设置为当前时间,每次修改model,都会自动更新
    modified_time = models.DateTimeField('修改时间', auto_now=True)

    class Meta:
        verbose_name = '文章'
        verbose_name_plural = '文章'

    def __str__(self):
        return self.title
class comment(models.Model):
    user = models.CharField('用户', max_length=50)
    content =models.TextField('评论', max_length=200, blank=True)

    replyUser = models.CharField('回复', max_length=50)

    STATUS_CHOICES = (
        ('d', '未回复'),
        ('p', '已回复'),
    )
    status = models.CharField(verbose_name='状态', max_length=1, choices=STATUS_CHOICES, default='d')

    reply = models.TextField('回复', max_length=200, blank=True)

    created_time = models.DateTimeField('评论时间', auto_now_add=True)
    modified_time = models.DateTimeField('回复时间', auto_now=True)

    ArticleComment = models.ForeignKey(Article, on_delete=models.DO_NOTHING, 
                            verbose_name='文章评论',
                            blank=True, null=True,related_name="ArticleCommentKey")



    class Meta:
        verbose_name = '评论'
        verbose_name_plural = '评论'

    def __str__(self):
        return self.user

